package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmployeeQueryObject;
import cn.wolfcode.crm.qo.PageResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;

    //查询所有角色
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll() {
        //将数据转换成json格式
        List<Role> roles = roleService.selectAll();

        return roles;
    }



    @RequestMapping("view")
    public String list(){
        return "role/list";
    }
    //异步加载数据
    @RequestMapping("query")
    @ResponseBody
    public Object list(@ModelAttribute("qo") QueryObject qo) {
        //将数据转换成json格式
        PageResult result = roleService.queryForList(qo);

        return result;
    }

    @RequestMapping("permissionIds")
    @ResponseBody
    public Object selectPermissionById(Long id) {
        //将数据转换成json格式
       Long[]ids = roleService.selectPermissionById(id);


        return ids;
    }
    //设置编辑菜单回显
    @RequestMapping("selectMenuByRoleId")
    @ResponseBody
    public Object selectMenuByRoleId(Long id) {
        //将数据转换成json格式
       Long[]ids = roleService.selectMenuByRoleId(id);


        return ids;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Role entity, Long[]menuIds){

      roleService.saveOrUpdate(entity,menuIds);
        return new JSONResult();
    }
    @ResponseBody
    @RequestMapping("delete")
    public Object delete(Long id){

       roleService.delete(id);
        JSONResult json = new JSONResult();
        return json;
    }
}
